<template>
<view>
	<!-- 内容区 -->
	<view class="tab-content">
		<view class="uni-common-mt">
			<uni-segmented-control :current="current" :values="tabBars" style-type="button"
				active-color="#18bc9c" @clickItem="onClickItem" />
		</view>
		
		<!-- 滑块视图 -->
		<view v-show="current === 0">
			<tableHead prestr="OIN" :order_type="11" ref="thead" :sel_source="false"></tableHead>
		</view>
		
		<view v-show="current === 1">
			<scanInvShelves :realQuantityZeroFlag="false" :toSuccessTip="true" :order_type="11" flag="in" ref="scanIS"></scanInvShelves>
			<view class="swiper-button">
				<button @click="checkOrder">完成</button>
				<button @click="$refs.scanIS.toAdd()" >添加</button>
			</view>
		</view>
		
		<view v-show="current === 2">
			<scroll-view scroll-y="true" style="height: 80vh;">
				<view class="form_list">
					<showMaterialList :dataArr="porderItem" :orderType="11" flag='in' :showShelves="false" :showShelvesName="true" ref="showMaterialList"></showMaterialList>
				</view>
			</scroll-view>
			
			<view class="swiper-button">
				<button @click="toMaterialList">添加物料</button>
				<button @click="checkOrder">完成</button>
			</view>
		</view>
	</view>
</view>
</template>

<script>
	import showMaterialList from "@/component/show-material-list.vue"
	import tableHead from "@/component/table-head.vue"
	import scanInvShelves from "@/component/scan-inv-shelves.vue"
	
	import {
		uniSegmentedControl
	} from '@dcloudio/uni-ui'
	
	export default {
		components: {
			uniSegmentedControl,showMaterialList,tableHead,scanInvShelves
		},
		data() {
			return {
				current: 1, /* 选中标签栏的序列,默认显示第一个 */
				tabBars:['表头','扫描','表体'],
				
				porderItem:[],//采购单表体
				
				user:uni.getStorageSync('user')
			}
		},
		computed: {},
		onShow() {
			
		},
		onLoad(options) {
			this.$Func.setBarTitle('新增其他入库')
			//初始化
			setTimeout(()=>{
				this.$refs.thead.init() //初始化
			},500)
			let _this = this
			
			/* 监听传过来的明细 */
			uni.$off("checkItems")
			uni.$on("checkItems", (value) => {
				this.porderItem = value;
			})
		},
		methods: {
			//切换标签内容
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex
				}
			},
			//选择物料
			toMaterialList() {
				this.globalData.toSecPage = 'inventory' //进入二级页
			 	uni.navigateTo({
			 		url: '/pages/inventory/index?flag=add'
			 	})
			},
			//核实入库单数据
			async checkOrder() {
				let _this = this;
				let check = []
				let check_total = 0 //总数不能为0
				let billentry = []
				let check_invclass_arr = []
				
				if(!this.porderItem.length){
					this.$Api.msg('表体不能为空','error')
					return
				}
				
				if (JSON.stringify(this.$refs.thead.department) == "{}" || this.$refs.thead.department.fName == '') {
					this.$Api.msg('请选择部门','error');
					return;
				}
					
				for(let i in this.porderItem){
					let citem = this.porderItem[i]
					//没输入数量的那些就是undefined
					if(citem.real_quantity == 0 || isNaN(citem.real_quantity)){
						this.$Api.msg(citem.name+'入库数量有误')
						return
					}
					
					if(citem.shelves_id == ''||citem.shelves_id == 0) {
						this.$Api.msg(citem.name+'目标货架、库区不能为空')
						setTimeout(()=>{
							this.$refs.showMaterialList.toGetShelves(i,0)
						},1000)
						return
					}
					
					//物料属性有设置批号
					if(citem.mbatch&&this.$Func.trim(citem.cBatch)==''){
						this.$Api.msg(citem.inv_name+"有启用批号属性,批号不能为空")
						return 
					}
					
					check_total += parseFloat(citem.real_quantity)//累加总数
					
					//判断货架物料分类策略数组
					check_invclass_arr.push({
						'name':citem.inv_name,
						'number':citem.inv_number,
						'shelves_id':citem.shelves_id,
						'classification_id':citem.classification_id
					})
					
					//单据表体添加
					billentry.push({
						'FBillNo':this.$refs.thead.FBillNo,
						'row_id':i,
						'local_row_id':i,
						'inv_number':citem.number,
						'inv_name':citem.name,
						'inv_specification':citem.specification,
						'inv_attributes':citem.attributes,
						'inv_unit':citem.inv_unit,
						'inv_unit_code':citem.inv_unit_code,
						'store_unit':citem.store_unit,
						'store_unit_code':citem.store_unit_code,
						'fqty':citem.real_quantity,
						'fauxqty':citem.real_store_quantity,//常用单位数量
						'cBatch':citem.cBatch,
						'shelves_id':citem.shelves_id,
						'shelves_name':citem.shelves_name,
						'shelves_type':citem.shelves_type,
						'b_remark':''
					})
				}
				
				if(!check_total||!billentry.length||isNaN(check_total)){
					this.$Api.msg("入库数量全为0",'error')
					return 
				}
				
				check_total = this.$Func.decimalPattern(check_total)
				
				let bill = {//准备表头数据
					'order_type':11,
					'FBillNo':this.$refs.thead.FBillNo,
					'FDate':this.$refs.thead.FDateVal,
					'a_remark':this.$refs.thead.remark,
					'stock_id':this.$refs.thead.stock_id,
					'factory_code':this.$refs.thead.department.fItemId,
					'factory_name':this.$refs.thead.department.fName,
					'status':1,
					'syn_status':0,
					'total_qty':check_total,
					'userid':this.user.userid,
					'create_date':this.$Func.getDateTime('today','Y-m-d')
				}
				
				let repeatFlag = this.$Func.checkSameShelvesInvOut(billentry,'boolean')
				if(repeatFlag){
					uni.showModal({
						title: '温馨提醒',
						content: '存在多条物料+货架相同记录，是否继续？',
						success: function (res) {
							if (res.confirm) {
								_this.addOrder(bill,billentry,check_invclass_arr)
							}
						}
					});
				}else{
					this.addOrder(bill,billentry,check_invclass_arr)
				}
			},
			//添加本地单据
			async addOrder(bill,billentry,check_invclass_arr){
				//判断货架物料分类策略,得放这里，这里才有classification_id
				// let msg = ''
				// await this.$Db.checkShelvesInvclass(check_invclass_arr).catch(err=>{
				// 	msg = err
				// 	this.$Db.addlog('添加其他入库',err)
				// })
				
				// if(msg){
				// 	this.$Api.msg(msg)
				// 	return
				// }
				
				uni.showLoading({mask:true})
				await this.$Db.addOrder(11,bill,billentry).then(res=>{
					this.$Api.msg('添加成功','success')
					this.reset()
					uni.hideLoading();
				}).catch(err=>{
					this.$Db.addlog('其他入库',err)
					this.$Api.msg('添加失败','error')
					uni.hideLoading();
				})
			},
			//重置表单
			reset() { 
				this.porderItem = [];
				this.$refs.thead.reset() //初始化
				this.$refs.scanIS.reset(true)//表体数组也一起清除
			}
		}
	}
</script>
<style lang="scss">
</style>